using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WPCoco.Tasks;
using System.Reflection;

namespace WPCoco.Helpers
{
    public static class TaskHelper
    {
        public static void ExpandTarget(Task task, object target) 
        {
            Type type = task.GetType();
            PropertyInfo pinfo = type.GetProperty("Target", BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.FlattenHierarchy);
            if (pinfo != null && pinfo.PropertyType.IsAssignableFrom(target.GetType()))
                pinfo.SetValue(task, target, null);
            IContainer<Task> composed = task as IContainer<Task>;
            if (composed != null) 
            {
                foreach (var t in composed.GetContainerEnumerator())
                    ExpandTarget(t, target);
            }
        }
    }
}
